#define _CRT_SECURE_NO_WARNINGS 1
#include<math.h>

int Hightree(struct TreeNode* root)
{
    if (root == NULL)
    {
        return 0;
    }
    if (root->left == NULL && root->right == NULL)
    {
        return 1;
    }
    int h1 = Hightree(root->left);
    int h2 = Hightree(root->right);
    return (h1 > h2 ? h1 : h2) + 1;

}

bool isBalanced(struct TreeNode* root) {
    if (root == NULL)
    {
        return true;
    }
    int h1 = Hightree(root->left);
    int h2 = Hightree(root->right);
    if (abs(h1 - h2) > 1)
    {
        return false;
    }
    return isBalanced(root->left) && isBalanced(root->right);
}